//
//  StatusSpeedStrategyLastNLocations.m
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 2/14/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "StatusSpeedStrategyLastNLocations.h"
#import "RotatingNGLocation.h"
#import "NGLocation.h"
#import "NGUtility.h"

@implementation StatusSpeedStrategyLastNLocations

- (id) init {
    if (self = [super init]) {
    }
    return self;
}

- (id) initWithRLocations:(RotatingNGLocation *)rLocations {
    if (self = [super initWithLocations:rLocations]) {
    }
    return self;
}

- (float) calculateSpeed:(NGLocation *)currentLocation rLocations:(RotatingNGLocation *)rLocations {
    NSMutableArray * locations = [rLocations toList];
    if (locations == nil || [locations count] == 0)
        return 0;
    NSArray * locs = [NGUtility sort:locations comparator:^(id a, id b) {
        NSDate *first = [(NGLocation *)a tarihSaat];
        NSDate *second = [(NGLocation *)b tarihSaat];
        return [first compare:second];
    }];
    NGLocation * loc0 = [locs objectAtIndex:0];
    NGLocation * locLast = currentLocation;
    if ([loc0 isEqualTo:locLast])
        return 0;
    return [NGUtility speed:loc0 to:locLast];
}

+ (void) main:(NSArray *)args {
    /*
    NGLocation * l1 = [[[NGLocation alloc] init] autorelease];
    SimpleDateFormat * sdf = [[[SimpleDateFormat alloc] init:@"yyyy-MM-dd HH:mm:ss"] autorelease];
    [l1 setTarihSaat:[[sdf parse:@"2012-02-02 23:36:39"] time]];
    [l1 setLat:39.9138029];
    [l1 setLon:32.859368];
    NGLocation * l2 = [[[NGLocation alloc] init] autorelease];
    [l2 setTarihSaat:[[sdf parse:@"2012-02-02 23:37:24"] time]];
    [l2 setLat:39.9138029];
    [l2 setLon:32.859368];
    NGLocation * l3 = [[[NGLocation alloc] init] autorelease];
    [l3 setTarihSaat:[[sdf parse:@"2012-02-02 23:38:14"] time]];
    [l3 setLat:39.9138029];
    [l3 setLon:32.859368];
    NGLocation * l4 = [[[NGLocation alloc] init] autorelease];
    [l4 setTarihSaat:[[sdf parse:@"2012-02-02 23:39:05"] time]];
    [l4 setLat:39.913717250000005];
    [l4 setLon:32.8593862];
    NGLocation * l5 = [[[NGLocation alloc] init] autorelease];
    [l5 setTarihSaat:[[sdf parse:@"2012-02-02 23:39:55"] time]];
    [l5 setLat:39.9138029];
    [l5 setLon:32.859368];
    NGLocation * l6 = [[[NGLocation alloc] init] autorelease];
    [l6 setTarihSaat:[[sdf parse:@"2012-02-02 23:40:45"] time]];
    [l6 setLat:39.9138029];
    [l6 setLon:32.859368];
    NGLocation * l7 = [[[NGLocation alloc] init] autorelease];
    [l7 setTarihSaat:[[sdf parse:@"2012-02-02 23:41:35"] time]];
    [l7 setLat:39.913717250000005];
    [l7 setLon:32.8593862];
    [l1 setSent:(char)1];
    RotatingNGLocation * lastNLocationList = [[[RotatingNGLocation alloc] init:10] autorelease];
    [lastNLocationList insertElement:l1];
    [l1 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l2];
    [l2 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l3];
    [l3 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l4];
    [l4 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l5];
    [l5 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l6];
    [l6 calculateSpeedAndStatus:lastNLocationList];
    [lastNLocationList insertElement:l7];
    [l7 calculateSpeedAndStatus:lastNLocationList];
    
    for (NGLocation * ngLocation in [lastNLocationList toList]) {
        [System.out println:[ngLocation stringByAppendingString:@"\n---\n"]];
    }
     */
    
}

@end
